from sms_rest.models import RestMessage
import hashlib
import os
import xadmin

class RestMessageAdmin(object):
    list_display = ['id','app_from','token','create_time']
    readonly_fields = ['token']

    def save_models(self):
        obj = self.new_obj
        qs = RestMessage.objects.filter(id=obj.id)
        if len(qs)==0:
            obj.token = hashlib.sha1(os.urandom(24)).hexdigest()
        obj.save()

xadmin.site.register(RestMessage,RestMessageAdmin)